// map.js
import { getdetailrepairAPI , deleterepaireAPI } from "../../../api/repair"
import qqmapsdk from "../../../utils/map"
Page({
  data: {
    latitude: 40.060539,
    longitude: 116.343847,
    form: {},
    haha:123,
    polyline:[],
    marker:[
      {
        id:1,
        latitude: 40.060539,
        longitude: 116.343847,
      },
      {
        id:2,
        latitude: 40.064239,
        longitude: 116.343847,
        iconPath:'../../../static/images/marker.png'
      }
    ],
  },
  onLoad(ev) {
    // console.log(ev.id);
    this.getdetailrepair(ev.id)
    this.qqdirection()
  },
  // 调用腾讯位置组件，骑行自行车路线规划
  qqdirection() {
    qqmapsdk.direction({
      mode:'bicycling',
      from:{latitude: 40.064239,longitude: 116.343847,},
      to:{latitude: 40.060539,longitude: 116.343847,},
      success:res =>{
        console.log(res.result.routes[0].polyline);
        const coors = res.result.routes[0].polyline
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        const kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        console.log(coors);
        const pl = []
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl);
        this.setData({
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      },
      fail: err =>{
        console.log(err);
      }
    })
  },
  // 拿到页面传参回显详情
  async getdetailrepair(id) {
    const res = await getdetailrepairAPI(id)
    // console.log(res)
    this.setData({form : res.data})
  },
  // 取消报修
  async deleterepaire () {
    const id = this.data.form.id
      const res =  await wx.showModal({
        title:'温馨提示',
        content:'您确定要删除吗？',
        confirmColor:'#5591af',
      })
      console.log(res.confirm)
      await deleterepaireAPI(id)
      wx.utils.toast('取消报修成功')
      setTimeout(() => {
        wx.redirectTo({
        url: '/repair_pkg/pages/list/index',
      }) 
      }, 2000);   
  },
  // 跳转编辑报修页面
  editform() {
    wx.redirectTo({
      url:'/repair_pkg/pages/form/index?id=' + this.data.form.id
    })
  },
})
